# Project: pyEnkido
# Module: font
# Author: Salwan A. AlHelaly

import sys, pygame, math
import image_utils
from pyenkido.font_defs import *
import xml.dom.minidom

class Font(object):
    def __init__(self):
        self.fontmap = image_utils.load_image("res/fonts", "font.png")
        self.fontmap.set_colorkey((0, 0, 0))
        self.defaultMask = self.fontmap.get_masks()

    def DrawTextASCII(self, screen, at, text):
        try:
            cursor = 0
            for char in text:
                item = ASCII_MAP[char]
                screen.blit(self.fontmap, (at[0]+(cursor*8),at[1]), pygame.Rect(item[0] * 7, item[1] * 7, 7, 7))
                cursor += 1
        except:
            raise

    def DrawTextEn(self, screen, at, textlist):
        cursor = 0
        for item in textlist:
            screen.blit(self.fontmap, (at[0]+(cursor*8),at[1]), pygame.Rect(item[0] * 7, item[1] * 7, 7, 7))
            cursor += 1

    def DrawTextAr(self, screen, at, textlist):
        cursor = 0
        for item in textlist:
            screen.blit(self.fontmap, (at[0]+(cursor*7),at[1]), pygame.Rect(item[0] * 7, item[1] * 7, 7, 7))
            cursor += 1

    def DrawNumberAr(self, screen, at, number, places = 0):
        sn = str(number)
        textlist = []
        for c in sn:
            textlist.append((int(c), 4))
        if places > len(textlist):
            temp = []
            for i in range(0, places - len(textlist)):
                temp.append((0, 5))
            temp.extend(textlist)
            textlist = temp
        self.DrawTextAr(screen, at, textlist)

    # Draws arabic characters Right-to-Left
    # at: is still the left-top corner
    def DrawTextArRL(self, screen, at, textlist):
        cursor = len(textlist)-1
        for item in textlist:
            screen.blit(self.fontmap, (at[0]+(cursor*7),at[1]), pygame.Rect(item[0] * 7, item[1] * 7, 7, 7))
            cursor -= 1

    def DrawCopyright(self, screen, at):
        screen.blit(self.fontmap, at, pygame.Rect(237,0,8,8))

    def SetColorMask(self, b_red, b_green, b_blue):
        m = (self.defaultMask[0] * b_red, self.defaultMask[1] * b_green, self.defaultMask[2] * b_blue, 0)
        self.fontmap.set_masks(m)

    def ResetColorMask(self):
        self.fontmap.set_masks(self.defaultMask)


class EnkidoFont(Font):
    def __init__(self, font_file):
        try:
            font_dom = xml.dom.minidom.parse("res/fonts/" + font_file)
            font_elements = font_dom.getElementsByTagName("font")
            if len(font_elements) > 0:
                dom = font_elements[0]
                att_w = dom.getAttribute("charwidth")
                att_h = dom.getAttribute("charheight")
                att_r = dom.getAttribute("charsperrow")
                att_t = dom.getAttribute("texture")
                if att_w == '' or att_h == '' or att_r == '' or att_t == '':
                    raise "Error parsing font element."
                self.charwidth = int(att_w)
                self.charheight = int(att_h)
                self.charsperrow = int(att_r)
                self.fontmap = image_utils.load_image("res/fonts", att_t)
                self.defaultMask = self.fontmap.get_masks()
                self.fontmap.set_colorkey((255, 0, 255))
            else:
                raise "Could not parse font elements."
        except:
            raise

    def DrawTextASCII(self, screen, at, text):
        try:
            cursor = 0
            for char in text:
                x = (ord(char) - 32) % self.charsperrow
                y = (ord(char) - 32) / self.charsperrow
                screen.blit(self.fontmap, (at[0]+(cursor*self.charwidth), at[1]),
                    pygame.Rect(x * self.charwidth, y * self.charheight, self.charwidth, self.charheight))
                cursor += 1
        except:
            raise

    def DrawTextEn(self, screen, at, textlist):
        self.DrawTextAr(screen, at, textlist)

    def DrawTextAr(self, screen, at, textlist):
        cursor = 0
        for n in textlist:
            x = n % self.charsperrow
            y = n / self.charsperrow
            screen.blit(self.fontmap,
                (at[0] + (cursor * self.charwidth), at[1]),
                pygame.Rect(x * self.charwidth, y * self.charheight, self.charwidth, self.charheight))
            cursor += 1

    def DrawNumberAr(self, screen, at, number, places = 0):
        sn = str(number)
        textlist = []
        for c in sn:
            textlist.append(int(c) + 100)
        if places > len(textlist):
            temp = []
            for i in range(0, places - len(textlist)):
                temp.append(100)
            temp.extend(textlist)
            textlist = temp
        self.DrawTextAr(screen, at, textlist)

    # Draws arabic characters Right-to-Left
    # at: is still the left-top corner
    def DrawTextArRL(self, screen, at, textlist):
        cursor = len(textlist) - 1
        for n in textlist:
            x = n % self.charsperrow
            y = n / self.charsperrow
            screen.blit(self.fontmap,
                (at[0] + (cursor * self.charwidth), at[1]),
                pygame.Rect(x * self.charwidth, y * self.charheight, self.charwidth, self.charheight))
            cursor -= 1

    def DrawCopyright(self, screen, at):
        pass

